package Pages;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;

import java.util.List;
import java.util.Map;

import static Driver.DriverSingleton.*;
import static org.hamcrest.CoreMatchers.*;
import static org.hamcrest.MatcherAssert.assertThat;

public class RecordDetailPage extends BasePage{
    WebDriver driver = getChromeDriver();
    Map<String,Object> vars = getVars();

    public RecordDetailPage toRecordDetailPageFromList(){
        driver.findElement(By.cssSelector(".el-table__row:nth-child(1) .el-button:nth-child(1) > span")).click();
        waitForWindow(2000);
        assertThat(driver.getTitle(), anyOf(is("咨询记录详情"),is("咨询&求助记录详情")));
        return this;
    }

    //在详情页获取星级评价值
    public int getStarFromRecordDetail(){
        List<WebElement> elements1 = driver.findElements(By.xpath("//*[@id=\"app\"]/div/div[2]/div[2]/div/div[1]/div[1]/div[2]/div[2]/div"));
        int count = 0;
        for(WebElement element : elements1){
            if(element.getAttribute("style").equals("color: rgb(247, 186, 42);") ){
                count++;
            }
        }
        System.out.println(count);
        return count;
    }

    //在Detail中获取时间
    public String getTimeFromRecordList(){
        waitForWindow(2000);
        String counselingTime = driver.findElement(By.xpath("//*[@id=\"app\"]/div/div[2]/div[2]/div/div[1]/div[1]/div[2]/div[1]/p[2]")).getText();
        return counselingTime;
    }

    public void downloadRecord() {
        driver.findElement(By.cssSelector(".btns > div:nth-child(1)")).click();
        String info = getDialog();
        assertThat( info, containsString("确认导出") );
        driver.findElement(By.cssSelector(".el-button--small:nth-child(2) > span")).click();

        waitForWindow(2000);
        System.out.println(driver.findElement(By.cssSelector(".content:nth-child(1)")).getText());

    }

    //获取详情页所有聊天记录
    public String getAllRecord() {
        String info = "";
        List<WebElement> elements = driver.findElements(By.xpath("//*[@id=\"app\"]/div/div[2]/div[2]/div/div[1]/div[2]/div/div/div/div[1]"));

        for (WebElement element:elements){
            info += element.getText();
            info += "\n";
        }
        System.out.println(info);
        return info;
    }
}
